from selenium import webdriver
from selenium.webdriver.common.by import By

class WebUtilDriver:

    _driver = None

    @classmethod
    def get_driver(cls):
        # 先判断内存中是否存在操作句柄
        if cls._driver is None:
            # 如果没有操作句柄，得创建一个操作句柄，并且进行赋值与返回
            cls._driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
            cls._driver.get('http://www.tp_shop.cn/')
            return cls._driver

        else:
            # 如果存在操作句柄，直接返回
            return cls._driver

    @classmethod
    def close_driver(cls):
        # 判断内存中，是否存在操作句柄
        # 如果不为空，则退出
        if cls._driver is not None:
            cls._driver.quit()
            cls._driver = None


# 用来定位登录弹窗的内容
def get_current_msg():
    current_msg = WebUtilDriver.get_driver().find_element(By.CSS_SELECTOR, '.layui-layer-content').text
    return current_msg
